
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.Socket;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import net.htmlparser.jericho.OutputDocument;
import net.htmlparser.jericho.Source;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author adn
 */
class conexion extends Thread{

    private Socket soc;
    private String archivo=null;
    private Fotograma fo = null;
    ObjectInputStream in;

    public conexion(Socket sok, String Archi) throws IOException {
        this.soc = sok;
        this.archivo= Archi;
         in= new ObjectInputStream(soc.getInputStream());
    }

    @Override
    public void run() {
        {
            try {
                FileWriter fstream = new FileWriter(archivo,true);
                BufferedWriter out = new BufferedWriter(fstream);

                try {
                    fo = (Fotograma) in.readObject();
                    out.write("<a href="+fo.Camara+"/index.html>"+fo.Camara+"</a><br>");
                    System.out.println("se agrego la camara al index");
                    out.close();
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex);
                }
                

            } catch (IOException ex) {
                Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex);
            }

            boolean flag = true;
            
            try {

                fo = (Fotograma) in.readObject();
                while(flag){
                if (fo != null){

                    File file = new File(fo.Camara);

                        if (file.exists()) {
                            while(flag){
                            fo = (Fotograma) in.readObject();
                            GuardarImagen(fo);
                            CambiarIndex(fo);
                            }
                        } else {
                            System.out.println("no existe la carpeta, creandola");

                            boolean gg = file.mkdir();
                            if (gg) {
                                System.out.println("Carpeta " + fo.Camara + " creada");
                            } else {
                                System.out.println("Carpeta " + fo.Camara + " no creada");
                            }
                        }

                        System.out.println(fo.Fecha);
                }
                }

                in.close();
                soc.close();
            } catch (ClassNotFoundException ex) {
                //Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Se cayo la conexion con la camara");
            } catch (IOException ex) {
                //Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Se cayo la conexion con la camara");
            }
        }

    }

    public void CambiarIndex(Fotograma ftgm) throws IOException {
        String sourceUrlString = ftgm.Camara + "/index.html";
        File   file            = new File(sourceUrlString);

        if (file.exists()) {
            Source         source         = new Source(new URL("file:"+sourceUrlString));
            OutputDocument outputDocument = new OutputDocument(source);

            outputDocument.replace(source.getElementById("imagen"),
                                   "<img id=\"imagen\" src=\"" + ftgm.Fecha + "\" alt=\"Angry face\" />");

            outputDocument.replace(source.getElementById("fecha"),
                                   "<h3 id=\"fecha\">"+ ftgm.Fecha + "</h3>");

            Writer out = new FileWriter(sourceUrlString);

            outputDocument.writeTo(out);
            out.close();
        } else {
            FileOutputStream fos;

            fos = new FileOutputStream(sourceUrlString);

            fos.flush();
            new PrintStream(fos).println ("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"es\" lang=\"es\" dir=\"ltr\">\n" + "<head>\n"
                + "<title>Cam center</title>\n"
                + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n"
                + "<meta name=\"keywords\" content=\"camaras web\"/>\n" + "<head>\n" + "<body>\n" + "<img id=\"imagen\" src=\""
                + ftgm.Fecha + "\" />\n" +"<br>"+"<h5 id=\"fecha\">"+ftgm.Fecha+"</h5>"+ "<a href=\"../index.html\">Volver</a>"+"</body>\n" + "</html>\n");
             System.out.println("se creo el index de la camara "+ ftgm.Camara);
        }
    }

    public void GuardarImagen(Fotograma ftgm) throws IOException {
        byte[]               bytesImagen   = ftgm.bytesImagen;
        ByteArrayInputStream entradaImagen = new ByteArrayInputStream(bytesImagen);
        BufferedImage        bufferedImage = ImageIO.read(entradaImagen);
        String               nombreFichero = ftgm.Camara + "/" + ftgm.Fecha;
        FileOutputStream     out           = new FileOutputStream(nombreFichero);

        ImageIO.write((RenderedImage) bufferedImage, "png", out);
    }
}